/*
 * @Copyright: Copyright (c) 2030 吴周海
 * @Author: wzh200x@126.com
 * @Date: 2025-10-16 14:44:43
 * @LastEditors: wzh200x@126.com
 * @LastEditTime: 2025-10-27 10:38:09
 * @FilePath: \gogamewebserver\goGameWebServer\internal\manager\platformManager.go
 * @Description: 文件功能描述
 */
package platformmanager

import (
	"goGameWebServer/internal/common/config"
	"goGameWebServer/internal/defines/platform"
	platforms "goGameWebServer/internal/platform"
	"sync"

	"github.com/gogf/gf/v2/container/gmap"
)

type PlatformManager struct {
	platformOptionsMap *gmap.StrAnyMap
	PlatformsMap       *gmap.IntAnyMap
}

func (p *PlatformManager) Init(options *config.PlatformOptions) {
	// 遍历把配置存储到map中,appid为key
	for _, platformOption := range options.Platforms {
		p.platformOptionsMap.Set(platformOption.AppId, &platformOption)
	}
	// 初始化平台操作对象
	p.PlatformsMap.Set(platform.WeChatGame, platforms.NewPlatformWechat())
	p.PlatformsMap.Set(platform.AliPayGame, platforms.NewPlatformAlipay())
	p.PlatformsMap.Set(platform.ByteJumpGame, platforms.NewPlatformByteJump())
	p.PlatformsMap.Set(platform.KuaiShouGame, platforms.NewPlatformKuaiShou())
	p.PlatformsMap.Set(platform.AliPayBaBaGame, platforms.NewPlatformAlipay())
	p.PlatformsMap.Set(platform.VivoGame, platforms.NewPlatformVivo())
	p.PlatformsMap.Set(platform.OppoGame, platforms.NewPlatformOppo())

}

func (p *PlatformManager) GetPlatformOption(appId string) *config.PlatformOption {
	if !p.platformOptionsMap.Contains(appId) {
		return nil
	}
	return p.platformOptionsMap.Get(appId).(*config.PlatformOption)
}

func (p *PlatformManager) GetPlatform(platformId int) platforms.IPlatform {
	if !p.PlatformsMap.Contains(platformId) {
		return nil
	}
	return p.PlatformsMap.Get(platformId).(platforms.IPlatform)
}

var PlatformManagerIns *PlatformManager
var oncePlatformManage sync.Once

func ManagerPlatform() *PlatformManager {
	oncePlatformManage.Do(func() {
		PlatformManagerIns = &PlatformManager{
			platformOptionsMap: gmap.NewStrAnyMap(),
			PlatformsMap:       gmap.NewIntAnyMap(),
		}
	})
	return PlatformManagerIns
}
